package com.lgl.service.impl;

import com.lgl.dao.ICodeDao;
import com.lgl.pojo.AuthCode;
import com.lgl.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CodeServiceImpl implements ICodeService {

    @Autowired
    private ICodeDao codeDao;

    @Override
    public String generateCode(String email) {
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        String sCode = String.valueOf(code);
        AuthCode authCode = new AuthCode();
        authCode.setCode(sCode);
        authCode.setCreatetime(new Date());
        codeDao.save(authCode);

        return sCode;
    }

    @Override
    public int validateCode(String email, String code) {
        List<AuthCode> byEmailAndCode = codeDao.findByCode(code);
        if (byEmailAndCode != null && byEmailAndCode.size() > 0) {
            return 0;
        } else {
            return 1;
        }
    }
}
